package com.example.myapplication;

import android.content.Context;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.util.AttributeSet;

import androidx.core.content.ContextCompat;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author ywz
 */
public class TimeTextView extends androidx.appcompat.widget.AppCompatTextView {

    private static final SimpleDateFormat FORMAT = new SimpleDateFormat("hh:mm:ss");
    /**
     * 倒计时的时间，还剩多少秒
     */
    private long mTime;
    /**
     * 当前的 textView 是否贴上窗口
     * true 已贴上
     * false 已移除
     */
    private boolean mIsAttached;
    private Context mContext;
    private int mNumRemain;

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        mIsAttached = false;
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        mIsAttached = true;
    }

    public TimeTextView(Context context) {
        super(context);
        init(context);
    }

    public TimeTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public TimeTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(context);
    }

    private void init(Context context) {
        this.mContext = context;
    }

    public void setTime(long mTime) {
        this.mTime = mTime;
    }

    public void setNumRemain(int mNumRemain) {
        this.mNumRemain = mNumRemain;
    }

    /**
     * 每秒更新时间
     *
     * @return true 没有时间，已过期，false 时间没有到期
     */
    public boolean updateCourseDetailsTime() {
        this.mTime = mTime - 1;
        if (mIsAttached) {
            StringBuilder sb = new StringBuilder();
            sb.append("剩余");
            sb.append(FORMAT.format(new Date(mTime * 1000)));
            setText(sb);
        }
        if (mTime <= 0) {
            return true;
        }
        return false;
    }

    /**
     * 课程订单详情
     *
     * @return true 没有时间，已过期，false 时间没有到期
     */
    public boolean updateCourseOrderDetailsTime() {
        this.mTime = mTime - 1;
        if (mIsAttached) {
            StringBuilder sb = new StringBuilder("仅剩");
            int s1 = sb.length();
            sb.append(mNumRemain);
            int s2 = sb.length();
            sb.append("个名额，");
            int s3 = sb.length();
            sb.append(FORMAT.format(new Date(mTime * 1000)));
            int s4 = sb.length();
            sb.append("后结束");
            int s5 = sb.length();
            SpannableString str = new SpannableString(sb);
            str.setSpan(new ForegroundColorSpan(ContextCompat.getColor(mContext, R.color.black28)),
                    0, s1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            str.setSpan(new ForegroundColorSpan(ContextCompat.getColor(mContext, R.color.red_5228)),
                    s1, s2, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            str.setSpan(new ForegroundColorSpan(ContextCompat.getColor(mContext, R.color.black28)),
                    s2, s3, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            str.setSpan(new ForegroundColorSpan(ContextCompat.getColor(mContext, R.color.red_5228)),
                    s3, s4, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            str.setSpan(new ForegroundColorSpan(ContextCompat.getColor(mContext, R.color.black28)),
                    s4, s5, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            setText(str);
        }
        if (mTime <= 0) {
            return true;
        }
        return false;
    }
}
